package fhp.hlhj.giantfold.activity.user
import android.app.Activity
import android.content.Intent
import android.text.InputFilter
import com.bumptech.glide.Glide
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.sharejz.ShareAppAty
import fhp.hlhj.giantfold.interfaces.IUser
import fhp.hlhj.giantfold.javaBean.UserInfoBean
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.presenter.ChangeInfoPresenter
import fhp.hlhj.giantfold.presenter.UserPresenter
import fhp.hlhj.giantfold.utils.SPUtils
import kotlinx.android.synthetic.main.activity_user_center.*

class UserCenter : BaseActivity(),IUser {

    private lateinit var presenter:UserPresenter
    override fun getContentId(): Int = R.layout.activity_user_center
    private val CODE=1;
    override fun initView() {
        presenter= UserPresenter(this)
        val httpParams = HttpParams()
        httpParams.put("user_id",Contents.userId)
        presenter.getUser(httpParams)
    }

    override fun showLoading() {
        loadingDialog.show()
    }

    override fun onResume() {
        super.onResume()
        val httpParams = HttpParams()
        if (SPUtils.readLoginType(this)!=-1){
            httpParams.put("user_id",Contents.userId)
            presenter.getUser(httpParams)
        }else{
            finish()
        }
    }
    override fun getUser(userInfoBean: UserInfoBean) {
        loadingDialog.dismiss()
        val data = userInfoBean.data
        if (userInfoBean.code==200){
            userName.text=data.nick_name
            tvAli.text=data.alipay_account
            Contents.aliAccount=data.alipay_account
            Contents.aliUserName=data.alipay_user_name
            Contents.availableMoeny=data.balance
            Glide.with(this).load(Urls.BaseUrl+"/"+data.head_pic).dontAnimate().placeholder(R.mipmap.placeholdericon).into(userIcon)
        }
    }
    override fun initListener() {
        btShare.setOnClickListener {
            startActivity(Intent(this, ShareAppAty::class.java))
        }
        btExit.setOnClickListener {
            finish()
        }
        btUser.setOnClickListener {
            startActivity(Intent(this, UserInfoAty::class.java))
        }
        btSetting.setOnClickListener {
            val intent = Intent(this, SettingsAty::class.java)
            startActivityForResult(intent,CODE)

        }
        btHelp.setOnClickListener {
            startActivity(Intent(this, HelpCenterAty::class.java))
        }
        btMyGain.setOnClickListener {
            startActivity(Intent(this, MyGainAty::class.java))
        }
        btZfb.setOnClickListener {
            if (Contents.aliAccount==null){
                startActivity(Intent(this, BindAliAty::class.java))
            }else{

            }
        }
        btGetCash.setOnClickListener {
            startActivity(Intent(this, GetCashAty::class.java))

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!= Activity.RESULT_OK)return
        if (requestCode==CODE){
            setResult(-1)
            finish()
        }
    }
}
